package com.ruoyi.project.system.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.security.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.LblCsCategory;
import com.ruoyi.project.system.service.ILblCsCategoryService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 首页轮播图Controller
 * 
 * @author BaoL
 * @date 2023-03-25
 */
@RestController
@RequestMapping("/system/category")
public class LblCsCategoryController extends BaseController
{
    @Autowired
    private ILblCsCategoryService lblCsCategoryService;

    /**
     * 头像上传
     */
    @Log(title = "图片上传", businessType = BusinessType.UPDATE)
    @PostMapping("/upload")
    public AjaxResult avatar(MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            String imgUrl = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
            return AjaxResult.success(imgUrl);

        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

    /**
     * 查询首页轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('system:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(LblCsCategory lblCsCategory)
    {
        startPage();
        List<LblCsCategory> list = lblCsCategoryService.selectLblCsCategoryList(lblCsCategory);
        return getDataTable(list);
    }

    /**
     * 导出首页轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('system:category:export')")
    @Log(title = "首页轮播图", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LblCsCategory lblCsCategory)
    {
        List<LblCsCategory> list = lblCsCategoryService.selectLblCsCategoryList(lblCsCategory);
        ExcelUtil<LblCsCategory> util = new ExcelUtil<LblCsCategory>(LblCsCategory.class);
        return util.exportExcel(list, "category");
    }

    /**
     * 获取首页轮播图详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(lblCsCategoryService.selectLblCsCategoryById(id));
    }

    /**
     * 新增首页轮播图
     */
    @PreAuthorize("@ss.hasPermi('system:category:add')")
    @Log(title = "首页轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LblCsCategory lblCsCategory)
    {
        return toAjax(lblCsCategoryService.insertLblCsCategory(lblCsCategory));
    }

    /**
     * 修改首页轮播图
     */
    @PreAuthorize("@ss.hasPermi('system:category:edit')")
    @Log(title = "首页轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LblCsCategory lblCsCategory)
    {
        return toAjax(lblCsCategoryService.updateLblCsCategory(lblCsCategory));
    }

    /**
     * 删除首页轮播图
     */
    @PreAuthorize("@ss.hasPermi('system:category:remove')")
    @Log(title = "首页轮播图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(lblCsCategoryService.deleteLblCsCategoryByIds(ids));
    }
}
